#include "Application.h"

//incializacion de attributos estaticos

Application* Application::instance = NULL;


Application::Application(){
}

Application::~Application(){}

bool Application::parseCommandLine(int argc,char* argv[]){
	if (argc == 2){
		filePath = argv[1];
		std::cout<<filePath<<std::endl;
		return true;
	}else{
		std::cout<<"Parametros ingresados invalidos no se puede iniciar la aplicacion."<<std::endl;
		return false;
	}
	return true;
}

int Application::run(){
	SDL_Event evento;
	int done=0;

	std::string msg = "Bienvenido a XMLParser, se inicia la aplicacion";
	
		Logger* logg = Logger::get_instance();
		logg->comenzar_log();
			
		logg->log(msg);

		XMLParser *parser = new XMLParser();

		list<MapList*> objects;

		try{
			parser->XMLParse(filePath, &objects);

			ObjectManager::getInstance()->generateElements(objects);

			Graficador::getInstance()->draw();

			while (done == 0){
				while (SDL_PollEvent(&evento)){
					if (evento.type == SDL_QUIT) {done = 1;}
					if (evento.type == SDL_KEYDOWN){
						switch (evento.key.keysym.sym){
							case SDLK_ESCAPE:
								done = 1;
							break;

							default:
								break;
						}
					}
				}
			}
			delete parser;
		}catch(ParsingExcep &e){
			logg->log(e);
			return RES_ERROR;
		}
		return RES_OK;
}

void Application::destroy(){

	Logger::destroy();
	TextureContainer::destroy();
	FigureContainer::destroy();
	FigureFactory::destroy();
	TextureFactory::destroy();
	ColorFactory::destroy();
	Graficador::destroy();
	DefaultParameterContainer::destroy();
	DefaultParameterManager::destroy();
	ObjectManager::destroy();
	SurfaceContainer::destroy();
	if(Application::getInstance!=NULL)
		delete Application::instance;
	Application::instance=NULL;
}

Application* Application::getInstance(){
	if(!Application::instance)Application::instance = new Application();
	return Application::instance;
}
